package com.wentao.mall.api

import com.wentao.mall.dao.OrderDao
import com.wentao.mall.dao.ProductDao
import com.wentao.mall.dao.UserDao
import com.wentao.mall.entity.PageQuery
import com.wentao.mall.entity.ProTableResult
import com.wentao.mall.entity.simple
import org.springframework.data.repository.findByIdOrNull
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RestController
import javax.persistence.Entity
import javax.persistence.GeneratedValue
import javax.persistence.GenerationType
import javax.persistence.Id

@RestController
class OrderController(
    val orderDao: OrderDao,
    val productDao: ProductDao,
    val userDao: UserDao,
) {
    @GetMapping("/admin/buyerOrder/list")
    fun list(pageQuery: PageQuery): ProTableResult {
        val page = orderDao.findAll(pageQuery.toPageRequest())
        return page.simple()
    }

    @PostMapping("/admin/buyerOrder/save")
    fun save(buyerOrder: BuyerOrder): Result {
        orderDao.save(buyerOrder.copy(productId = 2))
        return Result.success()
    }

    @PostMapping("/admin/buyerOrder/delete")
    fun delete(id: Int): Result {
        orderDao.deleteById(id)
        return Result.success()
    }

    data class CreateOrderParam(
        val openId: String? = null,
        val productId: Int? = null,
        val buyerName: String? = null,
        val buyerPhone: String? = null,
        val buyerAddressId: Int? = null,
        val buyerNote: String? = null,
    ) {

    }

    @PostMapping("/api/order/create")
    fun buy(@RequestBody createOrderParam: CreateOrderParam): Result {
        if (createOrderParam.productId == null || createOrderParam.openId == null) {
            return Result.error("null params")
        }
        val productId = createOrderParam.productId
        val openId = createOrderParam.openId

        val product = productDao.findByIdOrNull(productId) ?: return Result.error("product not found")
        val user = userDao.findByOpenid(openId) ?: return Result.error("user not found")

        val order = BuyerOrder(
            productId = productId,
            openId = openId,
            userId = user.id,
            price = product.price,
            productName = product.name,
            status = OrderStatus.PAID,
            buyerName = createOrderParam.buyerName,
            buyerPhone = createOrderParam.buyerPhone,
            buyerAddressId = createOrderParam.buyerAddressId,
            buyerNote = createOrderParam.buyerNote
        )

        val save = orderDao.save(order)
        user.points -= product.price!!
        userDao.save(user)

        return Result.success(save.id!!)

    }

    @GetMapping("/api/order/my")
    fun myOrders(openId: String, status: OrderStatus?): Result {

        val list = status?.let {
            if (it == OrderStatus.ALL) {
                orderDao.findByOpenId(openId)
            } else {
                orderDao.findByOpenIdAndStatus(openId, it)
            }
        } ?: let {
            orderDao.findByOpenId(openId)
        }

        return Result.success(list)
    }

}

@Entity
data class BuyerOrder(
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    val id: Int? = null,

    val merchantId: Int? = null,

    val productId: Int?,
    val openId: String?,
    val userId: Int?,

    val price: Int?,
    val productName: String?,

    val status: OrderStatus?,

    val buyerName: String? = null,
    val buyerPhone: String? = null,
    val buyerAddressId: Int? = null,
    val buyerNote: String? = null,

    )

enum class OrderStatus {
    NOT_PAID, PAID, USED, ALL
}